package com.uam.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.uam.common.annotation.Log;
import com.uam.common.constant.CST;
import com.uam.service.TaskService;
import com.uam.utils.HttpUtils;
import com.uam.web.common.model.BusinessType;
import com.uam.web.common.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author uam
 * @create 2023/12/3 15:02
 * @email uam@uam.com
 */
@Api(tags = "任务操作")
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @Log(title = "创建任务")
    @ApiOperation(value = "创建任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header", required = true),
            @ApiImplicitParam(name = "taskName", value = "任务名称"),
            @ApiImplicitParam(name = "airplaneId", value = "飞行器Id"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "startPoint", value = "出发地Id"),
            @ApiImplicitParam(name = "destination", value = "目的地Id"),
            @ApiImplicitParam(name = "mode", value = "飞行模式"),
            @ApiImplicitParam(name = "transitPoint", value = "途经点ID"),
    })
    @PostMapping("/create")
    @ResponseBody
    public Result create(HttpServletRequest request,
                         @RequestParam(required = true) String taskName,
                         @RequestParam(required = true) Long airplaneId,
                         @RequestParam(required = true) String startTime,
                         @RequestParam(required = true) Long startPoint,
                         @RequestParam(required = true) Long destination,
                         @RequestParam(required = true) Integer mode,
                         @RequestParam(required = false) List<Long> transitPoints,
                         @RequestParam(required = false)  String  rectanglePoint

    ) {
        return taskService.create(request, taskName, airplaneId, startTime, startPoint, destination, mode, transitPoints,rectanglePoint);
    }

    @Log(title = "批量文件上传")
    @ApiOperation(value = "批量上传接口-1文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header", required = true),
            @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = MultipartFile.class),
    })
    @PostMapping("/upload")
    @ResponseBody
    public Result uploadTaskFile(HttpServletRequest request, @RequestPart(value = "file") MultipartFile file

    ) {
        return taskService.uploadTaskFile(request, file);
    }

    @Log(title = "批量文件验证")
    @ApiOperation(value = "批量上传接口-2验证文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header", required = true),
            @ApiImplicitParam(name = "fileUrl", value = "文件地址,文件上传地址返回", dataTypeClass = String.class),
    })
    @PostMapping("/verify")
    @ResponseBody
    public Result verify(HttpServletRequest request, @RequestParam(required = true) String fileUrl

    ) {
        return taskService.verify(request, fileUrl);
    }

    @Log(title = "批量创建任务")
    @ApiOperation(value = "批量上传接口-3创建任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header", required = true),
            @ApiImplicitParam(name = "redisKey", value = "rediskey，存放任务的redisKey，验证文件接口返回", dataTypeClass = String.class),
    })
    @PostMapping("/createBatch")
    @ResponseBody
    public Result createBatch(HttpServletRequest request, @RequestParam(required = true) String redisKey,String rectanglePoint) {
        return taskService.createBatch(request, redisKey,rectanglePoint);
    }

    @Log(title = "查看任务列表", businessType = BusinessType.QUERY_LOG)
    @ApiOperation(value = "任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "Integer", defaultValue = "0", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", defaultValue = "10", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "status", value = "任务状态，0：待起飞、1：飞行中、2：避让、3：降落、4：完成、5：拒绝、6：取消", dataTypeClass = List.class),
            @ApiImplicitParam(name = "taskName", value = "任务名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createLoginName", value = "创建人名称（登录名称）", dataTypeClass = String.class),
    })
    @PostMapping("/list")
    // @PreAuthorize("@customExpression.hasAuthority('sys:user:list')")
    @ResponseBody
    public Result list(@RequestParam(required = false, defaultValue = "0") Integer pageNum,
                       @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) List<Integer> status,
                       @RequestParam(required = false) String taskName,
                       @RequestParam(required = false) String createLoginName
                       ) {
        return taskService.selectTaskList(pageNum, pageSize, status,taskName,createLoginName);
    }

    @Log(title = "任务编辑")
    @ApiOperation(value = "编辑任务接口-重新请求、取消")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "taskId", value = "任务id", dataType = "Integer", defaultValue = "0",
                    dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "status", value = "状态名称，重新起飞传：0；取消传：6", dataType = "Integer",
                    defaultValue = "0", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "startTime", value = "重新起飞时间，格式为：yyyyMMddHHmmss 例如：20231204170400," +
                    "如果重新起飞并且不传，则默认5分钟后起飞",
                    dataType = "Integer", dataTypeClass = String.class),

    })
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestParam(required = false) Integer taskId,
                       @RequestParam(required = false) Integer status,
                       @RequestParam(required = false) String startTime) {
        return taskService.edit(taskId, status, startTime);
    }


    @Log(title = "任务数据导出")
    @ApiOperation(value = "导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "startTime", value = "开始时间：格式为：yyyyMMddHHmmss 例如：20231204170400", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "结束时间：格式为：yyyyMMddHHmmss 例如：20231204170400", dataType = "String", dataTypeClass = Integer.class),

    })
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response,
                       @RequestParam(required = false) String startTime,
                       @RequestParam(required = false) String endTime) {
        taskService.export(response, startTime, endTime);
    }


    @Log(title = "任务定位")
    @ApiOperation(value = "定位接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "taskId", value = "任务id", dataType = "Integer", defaultValue = "0",
                    dataTypeClass = Integer.class)

    })

    @PostMapping("/location")
    @ResponseBody
    public Result location(@RequestParam(required = false) Integer taskId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("task_id", taskId);

        return Result.createSuccess(HttpUtils.get(CST.URL + "/uam/route/track/position", jsonObject));
    }

    @Log(title = "任务回放")
    @ApiOperation(value = "回放")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "taskId", value = "任务id", dataType = "Integer", defaultValue = "0",
                    dataTypeClass = Integer.class),
    })
    @PostMapping("/palyback")
    @ResponseBody
    public Result palyback(@RequestParam(required = false) Integer taskId) {
        //TODO 访问世峰接口
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("task_id", taskId);

        return Result.createSuccess(HttpUtils.get(CST.URL + "/uam/route/track/detail", jsonObject));
    }
}
